package com.chuangsi.project.admin.controller.app;



import com.chuangsi.common.enums.CataLogEnum;
import com.chuangsi.common.utils.ConfigUtil;
import com.chuangsi.common.utils.FileUploadUtil;
import com.chuangsi.common.utils.ImageUtil;
import com.chuangsi.framework.web.domain.ResultObject;
import com.chuangsi.project.admin.dto.common.ReqFileDto;
import com.chuangsi.project.admin.dto.common.ResFileDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Administrator
 */
@Api(tags = "APP-文件上传")
@RestController
@RequestMapping("/userApi/common")
public class AppFileController {

    @Autowired
    ConfigUtil configUtil;
    @Autowired
    FileUploadUtil uploadUtil;

    @ApiOperation(value = "图片上传", notes = "图片上传")
    @RequestMapping(value = "uploadImage", method = RequestMethod.POST)
    public ResultObject<ResFileDto> uploadImage(@Valid ReqFileDto dto){
        String name = uploadUtil.upload(dto.getFile(), CataLogEnum.IMAGE.getValue());
        ResFileDto add = new ResFileDto();
        add.setImgName(dto.getFile().getOriginalFilename());
        add.setImgUrl(CataLogEnum.IMAGE.getValue()+name);
        return new ResultObject<ResFileDto>().setData(add);
    }


    @ApiOperation(value = "图片上传（可缩放）", notes = "图片上传（可缩放）")
    @RequestMapping(value = "uploadImageZoom", method = RequestMethod.POST)
    public ResultObject<ResFileDto> uploadImageZoom(@Valid ReqFileDto dto){
        String name = uploadUtil.upload(dto.getFile(), CataLogEnum.IMAGE.getValue());
        ResFileDto add = new ResFileDto();
        if(dto.getProportion()!=null){
            String name1 = ImageUtil.zoomImage(
                    new File(configUtil.getPath() + CataLogEnum.IMAGE.getValue() + name),
                    configUtil.getPath() + CataLogEnum.IMAGE.getValue(),
                    dto.getProportion());
            add.setCmpImgName(name1);
            add.setCmpImgUrl(CataLogEnum.IMAGE.getValue()+name1);
        }
        add.setImgName(dto.getFile().getOriginalFilename());
        add.setImgUrl(CataLogEnum.IMAGE.getValue()+name);
        return new ResultObject<ResFileDto>().setData(add).setMsg("图片上传成功");
    }
    /**
     * 该方法可以上传文件，（wgt，等）
     * 也可以富文本文件上传
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "文件上传", notes = "文件上传")
    @PostMapping("/layeditUpload")
    public ResultObject<Map<String, Object>> layeditUpload(@Valid MultipartFile file) {
        if (file == null) {
            return ResultObject.error("上传文件不能为空");
        }
        String name = uploadUtil.upload(file, CataLogEnum.IMAGE.getValue());
        Map<String, Object> map = new HashMap<>(3);
        map.put("title", file.getOriginalFilename());
        map.put("src", configUtil.getUrl() + CataLogEnum.IMAGE.getValue() + name);
        long size = file.getSize();//kb
        //转换成//kb
        double sizeDouble = size / 1024;
        sizeDouble = sizeDouble / 1024;//转换成//MB
        //保留两位小数
        BigDecimal bg = new BigDecimal(sizeDouble);
        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        map.put("sizeDouble", f1 + "MB");
        return ResultObject.ok("上传成功").setData(map);
    }

    /**
     * 文件地址前缀
     *
     * @return 文件地址前缀
     */
    @ApiOperation(value = "获取文件地址前缀", notes = "获取文件地址前缀")
    @PostMapping("/getBaseUploadUrl")
    public ResultObject<Map<String, Object>> getBaseUploadUrl() {
        Map<String,Object> data = new LinkedHashMap<>();
        data.put("baseUploadUrl",configUtil.getUrl());
        return ResultObject.ok("查询成功").setData(data);
    }

}
